/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package planoaplicacaopoupanca;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author Marcela
 */
public class HelloMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command itemCommand1;
    private Command itemCommand;
    private Command itemCommand2;
    private Command irCommand;
    private Command voltarCommand;
    private Command CalcularCommand;
    private Command itemCommand3;
    private Command itemCommand4;
    private Form form;
    private TextField depositoMensaltextField;
    private TextField taxaMensaltextField;
    private TextField numeroMesestextField;
    private StringItem montanteStringItem;
    private Form logopoupancaprogramada;
    private ImageItem imageItem;
    private Form DescriçãodoPrograma;
    private StringItem stringItem;
    private Image image;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public HelloMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getLogopoupancaprogramada());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == DescriçãodoPrograma) {//GEN-BEGIN:|7-commandAction|1|54-preAction
            if (command == voltarCommand) {//GEN-END:|7-commandAction|1|54-preAction
                // write pre-action user code here
                switchDisplayable(null, getForm());//GEN-LINE:|7-commandAction|2|54-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|19-preAction
        } else if (displayable == form) {
            if (command == exitCommand) {//GEN-END:|7-commandAction|3|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|4|19-postAction
                // write post-action user code here
            } else if (command == itemCommand1) {//GEN-LINE:|7-commandAction|5|35-preAction
               calcularMontante();
//GEN-LINE:|7-commandAction|6|35-postAction
                // write post-action user code here
            } else if (command == itemCommand3) {//GEN-LINE:|7-commandAction|7|59-preAction
                // write pre-action user code here
                switchDisplayable(null, getDescriçãodoPrograma());//GEN-LINE:|7-commandAction|8|59-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|9|47-preAction
        } else if (displayable == logopoupancaprogramada) {
            if (command == irCommand) {//GEN-END:|7-commandAction|9|47-preAction
                // write pre-action user code here
                switchDisplayable(null, getForm());//GEN-LINE:|7-commandAction|10|47-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|7-postCommandAction
        }//GEN-END:|7-commandAction|11|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|12|
    //</editor-fold>//GEN-END:|7-commandAction|12|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of form component.
     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            form = new Form("Plano de Aplica\u00E7\u00E3o", new Item[] { getDepositoMensaltextField(), getNumeroMesestextField(), getTaxaMensaltextField(), getMontanteStringItem() });//GEN-BEGIN:|14-getter|1|14-postInit
            form.addCommand(getExitCommand());
            form.addCommand(getItemCommand1());
            form.addCommand(getItemCommand3());
            form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return form;
    }
    //</editor-fold>//GEN-END:|14-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand ">//GEN-BEGIN:|32-getter|0|32-preInit
    /**
     * Returns an initiliazed instance of itemCommand component.
     * @return the initialized component instance
     */
    public Command getItemCommand() {
        if (itemCommand == null) {//GEN-END:|32-getter|0|32-preInit
            // write pre-init user code here
            itemCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|32-getter|1|32-postInit
            // write post-init user code here
        }//GEN-BEGIN:|32-getter|2|
        return itemCommand;
    }
    //</editor-fold>//GEN-END:|32-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand1 ">//GEN-BEGIN:|34-getter|0|34-preInit
    /**
     * Returns an initiliazed instance of itemCommand1 component.
     * @return the initialized component instance
     */
    public Command getItemCommand1() {
        if (itemCommand1 == null) {//GEN-END:|34-getter|0|34-preInit
            // write pre-init user code here
            itemCommand1 = new Command("Calcular", "<nulo>", Command.ITEM, 0);//GEN-LINE:|34-getter|1|34-postInit
            // write post-init user code here
        }//GEN-BEGIN:|34-getter|2|
        return itemCommand1;
    }
    //</editor-fold>//GEN-END:|34-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand2 ">//GEN-BEGIN:|38-getter|0|38-preInit
    /**
     * Returns an initiliazed instance of itemCommand2 component.
     * @return the initialized component instance
     */
    public Command getItemCommand2() {
        if (itemCommand2 == null) {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
            itemCommand2 = new Command("Item", Command.ITEM, 0);//GEN-LINE:|38-getter|1|38-postInit
            // write post-init user code here
        }//GEN-BEGIN:|38-getter|2|
        return itemCommand2;
    }
    //</editor-fold>//GEN-END:|38-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: depositoMensaltextField ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of depositoMensaltextField component.
     * @return the initialized component instance
     */
    public TextField getDepositoMensaltextField() {
        if (depositoMensaltextField == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            depositoMensaltextField = new TextField("Dep\u00F3sito mensal (em R$):", "", 32, TextField.DECIMAL);//GEN-LINE:|29-getter|1|29-postInit
            // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return depositoMensaltextField;
    }
    //</editor-fold>//GEN-END:|29-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: numeroMesestextField ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of numeroMesestextField component.
     * @return the initialized component instance
     */
    public TextField getNumeroMesestextField() {
        if (numeroMesestextField == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            numeroMesestextField = new TextField("N\u00FAmero de meses", "", 32, TextField.DECIMAL);//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return numeroMesestextField;
    }
    //</editor-fold>//GEN-END:|30-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: taxaMensaltextField ">//GEN-BEGIN:|31-getter|0|31-preInit
    /**
     * Returns an initiliazed instance of taxaMensaltextField component.
     * @return the initialized component instance
     */
    public TextField getTaxaMensaltextField() {
        if (taxaMensaltextField == null) {//GEN-END:|31-getter|0|31-preInit
            // write pre-init user code here
            taxaMensaltextField = new TextField("Taxa mensal (em %):", "", 32, TextField.ANY);//GEN-LINE:|31-getter|1|31-postInit
            // write post-init user code here
        }//GEN-BEGIN:|31-getter|2|
        return taxaMensaltextField;
    }
    //</editor-fold>//GEN-END:|31-getter|2|
    //</editor-fold>













    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: logopoupancaprogramada ">//GEN-BEGIN:|42-getter|0|42-preInit
    /**
     * Returns an initiliazed instance of logopoupancaprogramada component.
     * @return the initialized component instance
     */
    public Form getLogopoupancaprogramada() {
        if (logopoupancaprogramada == null) {//GEN-END:|42-getter|0|42-preInit
            // write pre-init user code here
            logopoupancaprogramada = new Form("Plano de Aplica\u00E7\u00E3o ", new Item[] { getImageItem() });//GEN-BEGIN:|42-getter|1|42-postInit
            logopoupancaprogramada.addCommand(getIrCommand());
            logopoupancaprogramada.setCommandListener(this);//GEN-END:|42-getter|1|42-postInit
            // write post-init user code here
        }//GEN-BEGIN:|42-getter|2|
        return logopoupancaprogramada;
    }
    //</editor-fold>//GEN-END:|42-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageItem ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of imageItem component.
     * @return the initialized component instance
     */
    public ImageItem getImageItem() {
        if (imageItem == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            imageItem = new ImageItem("", getImage(), ImageItem.LAYOUT_DEFAULT, "Poupan\u00E7a Programada", Item.PLAIN);//GEN-LINE:|43-getter|1|43-postInit
            // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return imageItem;
    }
    //</editor-fold>//GEN-END:|43-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image ">//GEN-BEGIN:|44-getter|0|44-preInit
    /**
     * Returns an initiliazed instance of image component.
     * @return the initialized component instance
     */
    public Image getImage() {
        if (image == null) {//GEN-END:|44-getter|0|44-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|44-getter|1|44-@java.io.IOException
                image = Image.createImage("/imagem/Banner Plano Aplicacao (poupan\u00E7a programada).png");
            } catch (java.io.IOException e) {//GEN-END:|44-getter|1|44-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|44-getter|2|44-postInit
            // write post-init user code here
        }//GEN-BEGIN:|44-getter|3|
        return image;
    }
    //</editor-fold>//GEN-END:|44-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: irCommand ">//GEN-BEGIN:|46-getter|0|46-preInit
    /**
     * Returns an initiliazed instance of irCommand component.
     * @return the initialized component instance
     */
    public Command getIrCommand() {
        if (irCommand == null) {//GEN-END:|46-getter|0|46-preInit
            // write pre-init user code here
            irCommand = new Command("Pr\u00F3ximo", Command.BACK, 0);//GEN-LINE:|46-getter|1|46-postInit
            // write post-init user code here
        }//GEN-BEGIN:|46-getter|2|
        return irCommand;
    }
    //</editor-fold>//GEN-END:|46-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: voltarCommand ">//GEN-BEGIN:|53-getter|0|53-preInit
    /**
     * Returns an initiliazed instance of voltarCommand component.
     * @return the initialized component instance
     */
    public Command getVoltarCommand() {
        if (voltarCommand == null) {//GEN-END:|53-getter|0|53-preInit
            // write pre-init user code here
            voltarCommand = new Command("Voltar", Command.ITEM, 0);//GEN-LINE:|53-getter|1|53-postInit
            // write post-init user code here
        }//GEN-BEGIN:|53-getter|2|
        return voltarCommand;
    }
    //</editor-fold>//GEN-END:|53-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: CalcularCommand ">//GEN-BEGIN:|56-getter|0|56-preInit
    /**
     * Returns an initiliazed instance of CalcularCommand component.
     * @return the initialized component instance
     */
    public Command getCalcularCommand() {
        if (CalcularCommand == null) {//GEN-END:|56-getter|0|56-preInit
            // write pre-init user code here
            CalcularCommand = new Command("Calcular", Command.OK, 0);//GEN-LINE:|56-getter|1|56-postInit
            // write post-init user code here
        }//GEN-BEGIN:|56-getter|2|
        return CalcularCommand;
    }
    //</editor-fold>//GEN-END:|56-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: DescriçãodoPrograma ">//GEN-BEGIN:|51-getter|0|51-preInit
    /**
     * Returns an initiliazed instance of DescriçãodoPrograma component.
     * @return the initialized component instance
     */
    public Form getDescriçãodoPrograma() {
        if (DescriçãodoPrograma == null) {//GEN-END:|51-getter|0|51-preInit
            // write pre-init user code here
            DescriçãodoPrograma = new Form("Descri\u00E7\u00E3o", new Item[] { getStringItem() });//GEN-BEGIN:|51-getter|1|51-postInit
            DescriçãodoPrograma.addCommand(getVoltarCommand());
            DescriçãodoPrograma.setCommandListener(this);//GEN-END:|51-getter|1|51-postInit
            // write post-init user code here
        }//GEN-BEGIN:|51-getter|2|
        return DescriçãodoPrograma;
    }
    //</editor-fold>//GEN-END:|51-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|52-getter|0|52-preInit
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|52-getter|0|52-preInit
            // write pre-init user code here
            stringItem = new StringItem("Plano de Aplica\u00E7\u00E3o", "Tamb\u00E9m chamado de Poupan\u00E7a Programada. Calcula o resultado de um plano de aplica\u00E7\u00E3o, com valores de dep\u00F3sito mensal e de taxa, inserir o valor do dep\u00F3sito mensal, o n\u00FAmero de meses previsto e a taxa mensal. O programa ir\u00E1 mostrar o montante ao t\u00E9rmino do prazo.");//GEN-LINE:|52-getter|1|52-postInit
            // write post-init user code here
        }//GEN-BEGIN:|52-getter|2|
        return stringItem;
    }
    //</editor-fold>//GEN-END:|52-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: montanteStringItem ">//GEN-BEGIN:|57-getter|0|57-preInit
    /**
     * Returns an initiliazed instance of montanteStringItem component.
     * @return the initialized component instance
     */
    public StringItem getMontanteStringItem() {
        if (montanteStringItem == null) {//GEN-END:|57-getter|0|57-preInit
            // write pre-init user code here
            montanteStringItem = new StringItem("Valor do montante:", "");//GEN-LINE:|57-getter|1|57-postInit
            // write post-init user code here
        }//GEN-BEGIN:|57-getter|2|
        return montanteStringItem;
    }
    //</editor-fold>//GEN-END:|57-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand3 ">//GEN-BEGIN:|58-getter|0|58-preInit
    /**
     * Returns an initiliazed instance of itemCommand3 component.
     * @return the initialized component instance
     */
    public Command getItemCommand3() {
        if (itemCommand3 == null) {//GEN-END:|58-getter|0|58-preInit
            // write pre-init user code here
            itemCommand3 = new Command("Sobre", Command.ITEM, 0);//GEN-LINE:|58-getter|1|58-postInit
            // write post-init user code here
        }//GEN-BEGIN:|58-getter|2|
        return itemCommand3;
    }
    //</editor-fold>//GEN-END:|58-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand4 ">//GEN-BEGIN:|61-getter|0|61-preInit
    /**
     * Returns an initiliazed instance of itemCommand4 component.
     * @return the initialized component instance
     */
    public Command getItemCommand4() {
        if (itemCommand4 == null) {//GEN-END:|61-getter|0|61-preInit
            // write pre-init user code here
            itemCommand4 = new Command("Item", Command.ITEM, 0);//GEN-LINE:|61-getter|1|61-postInit
            // write post-init user code here
        }//GEN-BEGIN:|61-getter|2|
        return itemCommand4;
    }
    //</editor-fold>//GEN-END:|61-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }


    public void calcularMontante(){
        try{
        double deposito = Double.parseDouble(depositoMensaltextField.getString());
        double taxa = Double.parseDouble(taxaMensaltextField.getString());
        int mes = Integer.parseInt(numeroMesestextField.getString());
        taxa = taxa /100;
        double montante = (deposito * (1+taxa)) * ((elevado((1+taxa), mes) - 1)/taxa);
        
        int indice;
        String resultado =""+montante;
        indice = resultado.indexOf("."); // pega o indice do ponto
        String casas = resultado.substring(indice,resultado.length());
        if(casas.length() < 3){

        }else{
            resultado = resultado.substring(0,resultado.indexOf("."));
            resultado+= casas.substring(0,3);
        }

        montanteStringItem.setText("\nR$ "+resultado);
        }catch(Exception ex){
            montanteStringItem.setText("\nFavor verificar se os campos estão preenchidos corretamente!!");
        }
    }

    public double elevado(double n, double p){
        double r=1;
        for(int c=1;c<=p;c++){
            r*=n;
        }
        return r;

    }
}
